package com.aglow.bluetoothspeaker.main.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.aglow.bluetoothspeaker.R;
import com.aglow.bluetoothspeaker.model.bean.BluetoothDeviceBean;
import com.aglow.bluetoothspeaker.presenter.lisnter.OnItemClickListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class BlueboothSearchAdapter extends RecyclerView.Adapter<BlueboothSearchAdapter.ViewHolder> {


    private final Context context;
    private final List<BluetoothDeviceBean> list;
    private final LayoutInflater inflater;
    private OnItemClickListener mItemClickListener;

    public BlueboothSearchAdapter(Context context, List<BluetoothDeviceBean> list) {

        this.context = context;
        this.list = list;
        inflater = LayoutInflater.from(context);
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.bluetooth_info_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
       viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               mItemClickListener.onItemClick(view);
           }
       });
        return viewHolder;

    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        BluetoothDeviceBean bluetoothDeviceBean = list.get(position);
        String Devicename = bluetoothDeviceBean.getDevice().getName();

        //判断是否有蓝牙名字 如果没有就显示蓝牙地址
        holder.tvDeviceName.setText(Devicename != null ? Devicename : bluetoothDeviceBean.getDevice().getAddress());
        holder.itemView.setTag(position);

    }

    public void setItemClickListener(OnItemClickListener mItemClickListener){
       this.mItemClickListener=mItemClickListener;
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_bluetooth_logo)
        ImageView ivBluetoothLogo;
        @BindView(R.id.tv_device_name)
        TextView tvDeviceName;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}

